package com.konsung.ksdc.manager.community;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.GroupNetBean;
import com.konsung.ksdc.bean.netbean.RegisterUserNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.common.utils.ToastUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by liangkun on 2017/5/17 0017.
 * 全部群成员展示
 */

public class GroupMemberListActivity extends AppCompatActivity {
    @Bind(R.id.tv_group_name)
    TextView tvGroupName; // 群聊名称
    @Bind(R.id.iv_group_back)
    ImageView ivBack; // 退出
    @Bind(R.id.gv_group_member)
    GridView gvGroup; // 退出
    private String groupId;
    private GroupNetBean group;
    private List<RegisterUserNetBean> groupUsers; //群组成员

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allgroup_member);
        ButterKnife.bind(this);
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        groupId = getIntent().getStringExtra("groupId");
        RequestUtils.findCommunityById(groupId, new RequestCallback() {
            @Override
            public void onError(Call call, Exception e) {
                ToastUtils.toastContent(GroupMemberListActivity.this, getString(R.string
                        .chat_faile));
            }

            @Override
            public void onResponse(ResponseBean responseBean) {
                group = responseBean.getEntity(GroupNetBean.class);
                groupUsers = group.getUsers();
                String groupName = group.getGroupName() + "(" + groupUsers.size() + ")";

                tvGroupName.setText(StringUtils.stringOmit(groupName));
                // 初始化群成员表格
                initGroupMemberView();
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 初始化群成员列表
     */
    private void initGroupMemberView() {
        gvGroup.setAdapter(new GroupMemberGridAdapter(groupUsers));
        gvGroup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                    int i, long l) {
                Intent intent = new Intent(GroupMemberListActivity.this, ContactIndexActivity
                        .class);
                Gson gson = new Gson();
                intent.putExtra("data", gson.toJson(groupUsers.get(i)));
                startActivity(intent);
            }
        });
    }
}
